package com.kitty.game.equip.service;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.ServerService;
import com.kitty.game.base.service.BagService;
import com.kitty.game.chat.service.ChatService;
import com.kitty.game.config.NPC;
import com.kitty.game.confirm.model.ZHJConfirm;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.npc.message.RespNpcContent;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.RespConfirm;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.util.NutMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

import static com.kitty.game.npc.model.NpcButton.*;


@Service
@Slf4j
public class ZHJService {
    @Autowired
    ServerService serverService;
    @Autowired
    FabaoService fabaoService;
    @Autowired
    RoleService roleService;
    @Autowired
    ChatService chatService;
    private int npcId = 992;
    LinkedHashMap<String, NutMap> data = new LinkedHashMap<>();


    public void init() {
        String[] names = {"混元金斗", "番天印", "阴阳镜", "定海珠", "金蛟剪", "九龙神火罩", "卸甲金葫"};
        String[] xiangxins = {"金", "木", "水", "火", "土"};
        int index = 0;
        for (String name : names) {
            for (String xiangxin : xiangxins) {
                index++;
                String content = "1级" + name + "(" + xiangxin + ")";
                NutMap nutMap = new NutMap();
                nutMap.setv("name", content);
                nutMap.setv("jifen", 50);
                data.put("ZHJ0000" + index, nutMap);
            }
        }
        for (String name : names) {
            for (String xiangxin : xiangxins) {
                index++;
                String content = "13级" + name + "(" + xiangxin + ")";
                NutMap nutMap = new NutMap();
                nutMap.setv("name", content);
                nutMap.setv("jifen", 2000);
                data.put("ZHJ0000" + index, nutMap);
            }
        }
        for (String name : names) {
            for (String xiangxin : xiangxins) {
                index++;
                String content = "20级" + name + "(" + xiangxin + ")";
                NutMap nutMap = new NutMap();
                nutMap.setv("name", content);
                nutMap.setv("jifen", 150000);
                data.put("ZHJ0000" + index, nutMap);
            }
        }

//        for (String name : names) {
//            for (String xiangxin : xiangxins) {
//                index++;
//                String content = "24级" + name + "(" + xiangxin + ")";
//                NutMap nutMap = new NutMap();
//                nutMap.setv("name", content);
//                nutMap.setv("jifen", 2222);
//                data.put("ZHJ0000" + index, nutMap);
//            }
//        }


    }


    public String pointZHJ() {
        String head = "充值积分可以在我这兑换好礼哦！！！";
        String tail = "[1级法宝/" + jifen_zhouhuajian_1.getKey()
                + "][13级法宝/" + jifen_zhouhuajian_13.getKey()
                + "]"+
//                "[20级法宝/" + jifen_zhouhuajian_20.getKey()
//                + "]" +
//                "[24级法宝/" + jifen_zhouhuajian_24.getKey() + "]" +
                "[离开]";
        return head + tail;
    }

    public void list(Role role, String content, NPC npc) {
        String head = "充值积分可以在我这兑换好礼哦！！！";
        int level = 0;
        if (content.equals(jifen_zhouhuajian_1.getKey())) {
            level = 1;
        } else if (content.endsWith(jifen_zhouhuajian_13.getKey())) {
            level = 13;
        } else if (content.endsWith(jifen_zhouhuajian_18.getKey())) {
            level = 18;
        } else if (content.endsWith(jifen_zhouhuajian_20.getKey())) {
            level = 20;
        } else if (content.endsWith(jifen_zhouhuajian_24.getKey())) {
            level = 24;
        }
        for (Map.Entry<String, NutMap> entry : data.entrySet()) {
            NutMap nutMap = entry.getValue();
            if (!nutMap.getString("name").contains(level + "级")) {
                continue;
            }
            head = head + "[" + nutMap.getString("name") + "/" + entry.getKey() + "]";
        }
        String tail = head + "[离开]";

        RespNpcContent respNpcContent = new RespNpcContent();
        respNpcContent.setNpcName(npc.getName());
        respNpcContent.setNpcId((int) npc.getId());
        respNpcContent.setContent(tail);
        respNpcContent.setNpcIcon(npc.getIcon());
        MessagePusher.pushMessage(role, respNpcContent);
    }


    public boolean buy(Role role, String msg) {
        NutMap nutMap = data.get(msg);
        if (nutMap == null) {
            return true;
        }
        role.setConfirm(new ZHJConfirm(msg));
        msg = "确认花费#R" + nutMap.getInt("jifen") + "#n积分,兑换#R" + nutMap.getString("name") + "#n？";
        RespConfirm respConfirm = new RespConfirm();
        respConfirm.setTips(msg);
        MessagePusher.pushMessage(role, respConfirm);
        return true;
    }

    public boolean besureBuy(Role role) {
        ZHJConfirm zhjConfirm = (ZHJConfirm) role.getConfirm();
        String msgId = zhjConfirm.getMsg();
        NutMap nutMap = data.get(msgId);
        if (nutMap == null) {
            return true;
        }
        short pos = SpringUtils.getBean(BagService.class).getPos(role, false);
        if (pos <= 0) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("背包已满，请整理背包！！"));
            return true;
        }
        int cost = nutMap.getInt("jifen");
        roleService.substractChargeScore(role, cost);
        String notice = null;
        int xiangxin = 0;
        if (nutMap.getString("name").contains("(金)")) {
            xiangxin = 1;
        } else if (nutMap.getString("name").contains("(木)")) {
            xiangxin = 2;
        } else if (nutMap.getString("name").contains("(水)")) {
            xiangxin = 3;
        } else if (nutMap.getString("name").contains("(火)")) {
            xiangxin = 4;
        } else if (nutMap.getString("name").contains("(土)")) {
            xiangxin = 5;
        }
        String temp = nutMap.getString("name");
        notice = "惊闻#R" + role.getName() + "#n玩家在#R周华健#n处兑换获得#R" + temp + "#n！真是可喜可贺！#51 #82";
        int level = 1;
        String equipname = temp.substring(2, temp.indexOf("("));
        if (nutMap.getString("name").contains("24")) {
            level = 24;
            equipname = temp.substring(3, temp.indexOf("("));
        } else if (nutMap.getString("name").contains("20")) {
            level = 20;
            equipname = temp.substring(3, temp.indexOf("("));
        } else if (nutMap.getString("name").contains("18")) {
            level = 18;
            equipname = temp.substring(3, temp.indexOf("("));
        } else if (nutMap.getString("name").contains("13")) {
            level = 13;
            equipname = temp.substring(3, temp.indexOf("("));
        }else if (nutMap.getString("name").contains("8")) {
            level = 8;
            equipname = temp.substring(3, temp.indexOf("("));
        }
        fabaoService.getArtifact(role, equipname, xiangxin, level);
        log.error("周华健兑换法宝==={}=={}", role.getName(), Json.toJson(nutMap, JsonFormat.compact()));
        chatService.sendAdnotice(notice);
        return true;
    }

}
